//
//  YJSuggestionViewController.m
//  Sports-Go
//
//  Created by rimi on 16/10/4.
//  Copyright © 2016年 YRL. All rights reserved.
//

#import "YJSuggestionViewController.h"
#import "MBHUDDIY.h"
@interface YJSuggestionViewController ()<UITextViewDelegate>
@property (nonatomic,strong) UITextView *textView;
@property (nonatomic,strong) UITextView *textLabel;
@property (nonatomic,strong) UIButton *commitButton;
@end

@implementation YJSuggestionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    //去掉导航栏返回字
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];
    self.title = @"意见反馈";
    
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:17],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationController.navigationBar.barTintColor = getColor(58, 206, 248, 1);
   [self addUI];
}

- (void)addUI{
    
    _textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 64, 375*bi, 200*bi)];
    
    _textView.font = [UIFont systemFontOfSize:18];
    _textView.delegate = self;
   // [_textView becomeFirstResponder];
    _textView.backgroundColor = [UIColor clearColor];
    
    _textLabel = [[UITextView alloc] initWithFrame:CGRectMake(0, 64, 375*bi, 30*bi)];
    _textLabel.font = [UIFont systemFontOfSize:14];
    _textLabel.text = @"请在此输入";
    _textLabel.textColor = [UIColor lightGrayColor];
    
    [self.view addSubview:_textLabel];
    [self.view addSubview:_textView];
    
    
    _commitButton = [[UIButton alloc] initWithFrame:RzCGRectMake(37, 300, 300, 50)];
    
    _commitButton.backgroundColor = [UIColor colorWithRed:236/255.0 green:236/255.0 blue:236/255.0 alpha:1];
    [_commitButton setTitle:@"提交" forState:0];
    [_commitButton setTitleColor: [UIColor whiteColor] forState:0];
    
    _commitButton.layer.cornerRadius = 5;
    _commitButton.layer.masksToBounds = YES;
    _commitButton.userInteractionEnabled = NO;
    [_commitButton addTarget:self action:@selector(commitTheComment) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_commitButton];

}

#pragma mark -- button
- (void)commitTheComment{
    
    if ([_textView.text isEqualToString:@""]) {
        
        [self warnWitherrorInfo:@"内容不能为空"];
    }else{
        [[MBHUDDIY getInstent]show];
        [self performSelector:@selector(warnWitherrorInfo:) withObject:@"提交成功,谢谢您的反馈" afterDelay:2];
      
    }
}

#pragma mark -- uitextview delegate

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    
    if (![text isEqualToString:@""])
        
    {
        _textLabel.hidden = YES;
        _commitButton.userInteractionEnabled = YES;
        _commitButton.backgroundColor = getColor(58, 206, 248, 1);
    }
    
    if ([text isEqualToString:@""] && range.location == 0 && range.length == 1)
    {
        
        _textLabel.hidden = NO;
        _commitButton.userInteractionEnabled = NO;
        _commitButton.backgroundColor = [UIColor lightGrayColor];
        
    }
    
    return YES;
}

#pragma mark -- alter
- (void)warnWitherrorInfo:(NSString *)error{
    //警示框
    UILabel *label = [[UILabel alloc]initWithFrame:RzCGRectMake(90, 250, 185, 50)];
    label.backgroundColor = [UIColor lightGrayColor];
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:16];
    label.numberOfLines = 0;
    label.text = error;
    label.layer.cornerRadius = 5;
    label.layer.masksToBounds = YES;
    label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label];
    [UIView animateWithDuration:1.0 animations:^{
        label.alpha = 0.0;
    [[MBHUDDIY getInstent]stop];
    } completion:^(BOOL finished) {
        
        [label removeFromSuperview];
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    //点击空白地方,键盘收回
    [_textView resignFirstResponder];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES];
    self.tabBarController.tabBar.hidden = NO;
}


@end
